#include <stdio.h>
#include <string.h>

int s, t, m;
int all[110], flag[110];
int icase=0;
bool in()
{
    int i, j;
    int id, a, b;
    scanf("%d%d%d", &s, &t, &m);
    if (s==0 && t==0 && m==0) return false;
    
    memset(all, 0, sizeof(all));
    memset(flag, 0, sizeof(flag));
    for (i=0; i<s; ++i)
    {
        scanf("%d%d%d", &id, &a, &b);
        if (a) all[id]++;
        if (b) all[id]++;
    }    
    for (i=0; i<t; ++i)
    {
        scanf("%d%d", &id, &a);
        if (a>=m)
                flag[id] = 1;
    }    
    return true;
}    

void output()
{
    int i, ans=0;
    for (i=1; i<=s; ++i)
        ans += flag[i]+all[i];
    printf("Case %d: %d\n", ++icase, ans);
}    
int main()
{
    //freopen("acmteam.in", "r", stdin);
    while (in())
    {
        output();
    }    
    

    
    return 0;    
}    